<?php

session_start();
/**
 * Description of login
 *
 * @author bilde
 *
 * This is the Login-controller which take control of handleling login,
 * and user-registration.
 */
class Login extends CI_Controller
{
    function  __construct()
    {
        parent::__construct();       
    }

    //The index() is generating the 'login_form-view', by passing it as main content,
    //to the overall 'template'. This idea is taken from the Thomas Meyer's Code Igniter book.
    function index()
    {
        $data['main_content'] = 'loginviews/login_form';
        $this->load->view('includes/template', $data);
    }

    //validate() is loading the model 'model_users' and passing the user-input,
    // to the 'model-users's 'validate'function.
    function validate_credentials()
    {

        $this->load->model('model_users');
        $Q = $this->model_users->validate();

        if($Q) // if the user's credentials validated...
        {
            $data = array(
                    'username' => $this->input->post('username'),
                    'is_logged_in' => true
            );

            $this->session->set_userdata($data);
            redirect('socialbuddy/index');
        }
        else // incorrect username or password
        {
                $this->index();
        }
    }

    function verify_admin()
    {
        if ($this->input->post('username'))
        {
            $u = $this->input->post('username');
            $pw = $this->input->post('password');
            $this->model_admins->verify_user($u,$pw);
            if ($this->session->userdata('userid') > 0)
            {
                redirect('admin/dashboard', 'refresh');
            }
        }

        $data['title'] = "Fully Jacked | Admin Login";
        $data['main_content'] = 'admin/admin_login';
        $this->load->vars($data);
	$this->load->view('includes/template');
    }

    //This controller take care of generating the 'signup-form',
    //and passing it as 'main:content' to the overall 'template'.
    function signup()
    {
        $data['main_content'] = 'signupviews/signup_form';
        $this->load->view('includes/template', $data);
    }

    //This function uses the code igniter 'form_validation'-libary, to set up,
    // the fields we require for a user to have, as a minimum.
    function create_user()
    {

        // field name, error message, validation rules
        $this->form_validation->set_rules('first_name', 'Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');

        //If something goes wrong running the 'form_validation'
        // You will be prompted the signup-form again.
        if($this->form_validation->run() == FALSE)
        {
            $this->signup();
        }

        //Else the controller will call the model 'model_users',
        // and if all required fields are set, and allright,
        // it will create the user, and send you forward to the 'signup_successful'.
        else
        {
            $this->load->model('model_users');

            if($Q = $this->model_users->create_user())
            {
                $data['main_content'] = 'signupviews/signup_successful';
                $this->load->view('includes/template', $data);
            }
            else //If fields are not set correctly.
            {
                $this->load->view('signupviews/signup_form');
            }
        }

    }

    //This function is to use on a link or button to destroy a session,
    // or what you can know as "Logout".
    function logout()
    {
        $this->session->sess_destroy();     
        redirect('welcome/index');
    }

}
?>
